package chances.epg.user.service.rdb;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import chances.epg.data.PageBean;
import chances.epg.data.ibatis.PageRowBounds;

public abstract class BaseDao {
	
	@Autowired
	protected SqlSessionTemplate userReadDBClient;
	
	@Autowired
	protected SqlSessionTemplate userWriteDBClient;
	/**
	 * 
	 * @param queryName
	 * @param paramsMap
	 * @return
	 */
	public Object queryForObject(String statementId,Map<String,Object> paramsMap) {
		return this.userReadDBClient.selectOne(statementId, paramsMap);
	}
	
	/**
	 * 
	 * @param statementId
	 * @return
	 */
	public List<?> queryForList(String statementId) {
		return this.userReadDBClient.selectList(statementId);
	}
	
	/**
	 * 
	 * @param queryName
	 * @param paramsMap
	 * @return
	 */
	public List<?> queryForList(String statementId,Map<String,Object> paramsMap) {
		return this.userReadDBClient.selectList(statementId,paramsMap);
	}
	
	/**
	 * 
	 * @param queryName
	 * @param paramsMap
	 * @param size
	 * @return
	 */
	public List<?> queryForList(String statementId,Map<String,Object> paramsMap,int start,int size) {
		PageRowBounds bounds = new PageRowBounds();
		bounds.setStart(start);
		bounds.setPageSize(size);
		return this.userReadDBClient.selectList(statementId,paramsMap,bounds);
	}
	
	/**
	 * 
	 * @param queryName
	 * @param paramsMap
	 * @param pageBean
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List<?> queryForList(String statementId,Map<String,Object> paramsMap,int start,PageBean pageBean) {
		PageRowBounds bounds = new PageRowBounds();
		bounds.setStart(start);
		bounds.setPageSize(pageBean.getPageSize());
		bounds.setPageNo(pageBean.getCurrentPage());
			
		List result = this.userReadDBClient.selectList(statementId, paramsMap, bounds);
		pageBean.setRecordCount(bounds.getRecordCount());
		return result;
	}
	
	/**
	 * 
	 * @param statementId
	 * @param paramsMap
	 * @return
	 */
	public int insert(String statementId,Map<String,Object> paramsMap) {
		return this.userWriteDBClient.insert(statementId, paramsMap);
	}
	
	/**
	 * 
	 * @param statementId
	 * @param paramsMap
	 */
	public void update(String statementId,Map<String,Object> paramsMap){
		this.userWriteDBClient.update(statementId, paramsMap);
	}
	
	/**
	 * 
	 * @param statementId
	 * @param paramsMap
	 */
	public void delete(String statementId,Map<String,Object> paramsMap){
		this.userWriteDBClient.delete(statementId, paramsMap);
	}
}
